package com.yunkeji.admin.controller.appuser;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.annotation.RepeatSubmit;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.core.validate.AddGroup;
import com.yunkeji.common.core.validate.EditGroup;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.vo.AppUserExtVo;
import com.yunkeji.system.domain.bo.AppUserExtBo;
import com.yunkeji.system.service.IAppUserExtService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 用户基本信息
 *
 * @author zhang
 * @date 2024-07-10
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/appUserExt")
public class AppUserExtController extends BaseController {

    private final IAppUserExtService iAppUserExtService;

    /** 查询用户基本信息列表 */
    @SaCheckPermission("system:appUserExt:list")
    @GetMapping("/list")
    public TableDataInfo<AppUserExtVo> list(AppUserExtBo bo, PageQuery pageQuery) {
        return iAppUserExtService.queryPageList(bo, pageQuery);
    }

    /** 导出用户基本信息列表 */
    @SaCheckPermission("system:appUserExt:export")
    @Log(title = "用户基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AppUserExtBo bo, HttpServletResponse response) {
        List<AppUserExtVo> list = iAppUserExtService.queryList(bo);
        ExcelUtil.exportExcel(list, "用户基本信息", AppUserExtVo.class, response);
    }

    /**
     * 获取用户基本信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:appUserExt:query")
    @GetMapping("/{id}")
    public R<AppUserExtVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(iAppUserExtService.queryById(id));
    }

    /** 新增用户基本信息 */
    @SaCheckPermission("system:appUserExt:add")
    @Log(title = "用户基本信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AppUserExtBo bo) {
        return toAjax(iAppUserExtService.insertByBo(bo));
    }

    /** 修改用户基本信息 */
    @SaCheckPermission("system:appUserExt:edit")
    @Log(title = "用户基本信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AppUserExtBo bo) {
        return toAjax(iAppUserExtService.updateByBo(bo));
    }

    /**
     * 删除用户基本信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:appUserExt:remove")
    @Log(title = "用户基本信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iAppUserExtService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 删除用户头像
     *
     * @param id 主键
     */
    @SaCheckPermission("system:appUserExt:remove")
    @Log(title = "删除用户头像", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteHeadUrl/{id}")
    public R<Void> deleteHeadUrl(@PathVariable Long id) {
        return toAjax(iAppUserExtService.deleteHeadUrl(id));
    }

    /**
     * 删除用户视频
     *
     * @param id 主键
     */
    @SaCheckPermission("system:appUserExt:remove")
    @Log(title = "删除用户头像", businessType = BusinessType.DELETE)
    @DeleteMapping("deleteVideo/{id}")
    public R<Void> deleteVideo(@PathVariable Long id) {
        return toAjax(iAppUserExtService.deleteVideo(id));
    }
}
